home *** CD-ROM | disk | FTP | other *** search
- Path: pegasus.montclair.edu!harmon
- From: harmon@pegasus.montclair.edu (Derek Harmon)
- Newsgroups: comp.lang.c
- Subject: Re: Can someone help me with fopen
- Date: 14 Feb 1996 09:05:50 -0500
- Organization: Montclair State University
- Message-ID: <harmon.824306709@pegasus.montclair.edu>
- References: <4fouoo$k0e@news.uncc.edu>
- NNTP-Posting-Host: pegasus.montclair.edu
- X-Newsreader: NN version 6.5.0 #68 (NOV)
-
- In comp.lang.c you write:
-
- >Here is my problem. I am trying to read a text file, search for a certain
- >string and write that string to an outfile. I am having trouble with the fopen
- >commnad. Here is a fragment of my code.
-
- >char input; /* variable to put filename in*/
- >FILE *infile /* Pointer to File to be used as input */
-
- : char input[13];
-
- input needs to be either a statically allocated array (as shown here, a
- length of 13 is suitable for a DOS filename w/o path, vary as required) or
- a pointer to a char in which case you must allocate with malloc().
-
- >if (argv[argc] == "-i")
- > {input = ((argv[argc]) + 1);
- > infile = fopen((("%s", input), "r"));
- > if ((infile = fopen((("%s", input), "r"))) == NULL)
- > {/*Error Message*/
-
- First, delete line #3 here as there is no need to open a file twice, and
- it is best done inside the if that tests for error conditions. Also be aware
- that argv[argc] is guaranteed to be NULL. argc is 1 for the path/name of the
- executing program, held in argv[0]. When argc is 3, argv[2] is the last
- argument from the command-line. Even if argv[] were the string you wanted,
- you cannot compare strings with the == operator, you must use strcmp(). For
- example, if (!strcmp(argv[1],"-i")) ... be aware that strcmp(a,b) returns 0
- when string a is equal to string b.
-
- >I keep getting an "too few arguments to function `fopen'
-
- The prototype for fopen() is,
-
- FILE *fopen(const char *filename, const char *mode);
-
- First, "%s" belongs with printf() and scanf() functions and their kin, and
- is what is known as a format specifier in the language. It will associate
- only with functions that take a variable number of arguments. fopen() is
- not such a function. What you should use,
-
- if ((infile = fopen("\\projects\\foo.dat", "rt")) == NULL) {
-
- The first parameter is a filename. It can be a char * or array of char,
-
- char filename[13];
- :
- if ((infile = fopen(filename, "wb+")) == NULL) {
-
- char *fname;
- :
- fname = (char *)malloc(24 * sizeof(char));
- strcpy(fname,"\\graphics\\tiger.jpg");
- if ((infile = fopen(fname, "rb")) == NULL) {
-
- There are a variety of modes, read/write, text/binary, open for update (+),
- that should be covered in any good manual or reference on C.
-
- >Please e-mail me is you have any idea on this problem(or any other aspect of
- > this program for that matter :>)
- >Any help would be greatly appreciated
-
- I tried to respond via E-Mail, but your address lacked a domain.
-
- -- Stone
- --
- # Derek Harmon (aka Stonelight) harmon@pegasus.montclair.edu
- # - Computer Science Undergrad, Montclair State University, NJ
- # - My views are my own, nobody else is this creative. 3;)>
- ... C programmer run, C programmer crash, Ada programmer.
-
-